import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;

public class Parser {

	public static Connection getConnection() throws InstantiationException, IllegalAccessException 
	{ Connection con = null;
		try { 
			String connectionURL = "jdbc:mysql://localhost:3306/mysql"; // test is my database name
	        Connection connection = null;
	        Class.forName("com.mysql.jdbc.Driver").newInstance();
			//String url = "jdbc:odbc:" + "//localhost:3036/test"; 
			// assuming "DataSource" is your DataSource name 
			//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
			try 
			{ 
				con=DriverManager.getConnection(connectionURL,"root",""); 
				// assuming your SQL Server's username is "username" 
				// and password is "password" 
				System.out.println("Connected");
			} 
				catch (SQLException ex) 
				{ 
					ex.printStackTrace(); 
				} 
				} 
					catch(ClassNotFoundException e) 
					{ 
						System.out.println(e); 
					} 
					return con; 
	 
		}  
	public static void main(String args[]) throws IOException, SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
	{ 
		int uidcount=0;
		FileReader fr = new FileReader("/Users/ajayreddyanireddy/Desktop/Final-Tf-Idf/part-r-00000");
		BufferedReader br = new BufferedReader(fr);
		String connectionURL = "jdbc:mysql://localhost:3306/search"; // test is my database name
		 Connection connection = null;
		String s;
		float tfidffloat;
		while((s = br.readLine()) != null) {
		
				uidcount++;
			String[] wordurlAndTfidf = s.split("\t");  //havnt included to string
			String[] wordAndurl =wordurlAndTfidf[0].split("@");
			 
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			  connection = DriverManager.getConnection(connectionURL, "root", "");
		    	//System.out.println("Reached start;;;;");
		  Statement stmt = connection.createStatement();
			  stmt = connection.createStatement();
			// connection = DriverManager.getConnection(connectionURL, "root", "");
			//Add wordAndurl[0] to database
			
			
			//String[] othersAndtfidf = wordurlAndTfidf[1].split(" , ");
			//String[] tfidf = othersAndtfidf[1].split(" , ");
			//System.out.println(othersAndtfidf[0]);
			  int index = wordurlAndTfidf[1].indexOf(".");
			  int index2 = wordurlAndTfidf[1].indexOf("]");
			  if (index!=-1 && index2!=-1)
			  {
				  String tfidf = wordurlAndTfidf[1].substring(index,index2);
				  //System.out.println(tfidf);
			//System.out.println(othersAndtfidf[1]);
			System.out.println(wordAndurl[1]);
			//tfidffloat = Float.parseFloat(tfidf);
			//tfidffloat = Float.valueOf(othersAndtfidf[1].trim()).floatValue();
			//Add tfidf[1] to database 
			
			//stmt.executeUpdate("INSERT INTO mysql"+"VALUES('" + uidcount + "','" + wordAndurl[0] + "','" + wordAndurl[1] + "')");
			//stmt.executeUpdate("INSERT INTO urldb"+"VALUES(0,'hi','.com',0.04)");
			//String update = "INSERT INTO urldb('" + uidcount + "','" + wordAndurl[0] + "','" + wordAndurl[1] + "')";
			PreparedStatement ps = connection.prepareStatement("INSERT INTO urldb (word,url,tfidf) VALUES (?,?,?)");
			//String update = "INSERT INTO urldb" + "(uid," + "word," + "url)" + "VALUES('" + uidcount + "','" + wordAndurl[0] + "','" + wordAndurl[1] + "')"; 
			//ps.setInt(1, uidcount);
			ps.setString(1, wordAndurl[0]);
			ps.setString(2, wordAndurl[1]);
			ps.setString(3,tfidf);
			
			ps.executeUpdate();
			
			  }
			 
		}
		 //System.out.println(uidcount);
		fr.close(); 


    
	}
	
}
